package com.cxg.fairy.ec.main.index;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ec.main.index.city.CityDelegate;
import com.cxg.fairy.ec.main.personal.message.MessageCenterDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.DrawableEditText;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.delegates.bottom.BottomItemDelegate;
import com.cxg.fairy.ec.main.EcBottomDelegate;
import com.cxg.fairy.ec.main.index.search.SearchDelegate;
import com.cxg.fairy.ui.recycler.BaseDecoration;
import com.cxg.fairy.ui.refresh.RefreshHandler;
import com.joanzapata.iconify.widget.IconTextView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class IndexDelegate extends BottomItemDelegate implements View.OnFocusChangeListener , ISuccess{

    @BindView(R2.id.rv_index)
    RecyclerView mRecyclerView = null;
    @BindView(R2.id.srl_index)
    SwipeRefreshLayout mRefreshLayout = null;
    @BindView(R2.id.tb_index)
    Toolbar mToolbar = null;
    @BindView(R2.id.icon_index_scan)
    IconTextView mIconScan = null;
    @BindView(R2.id.et_search_view)
    DrawableEditText mSearchView = null;

    private MultipleRecyclerAdapter mAdapter = null;
    private final DataConverter CONVERTER = new IndexDataConverter();

    @OnClick(R2.id.layout_index_city)
    void onClickChangeCity() {
        getParentDelegate().getSupportDelegate().start(new CityDelegate());
    }

    private RefreshHandler mRefreshHandler = null;

    @OnClick(R2.id.icon_index_scan)
    void onClickScanQrCode() {
        startScanWithCheck(this.getParentDelegate());
    }

    @OnClick(R2.id.icon_index_message)
    void onClickMessageCenter() {
        getParentDelegate().getSupportDelegate().start(new MessageCenterDelegate());
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mRefreshHandler = RefreshHandler.create(mRefreshLayout, mRecyclerView, CONVERTER);
        mSearchView.setOnFocusChangeListener(this);
    }

        private void initRefreshLayout() {
        mRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mRefreshLayout.setProgressViewOffset(true, 120, 300);
    }


    @SuppressLint("NewApi")
    private void initRecyclerView() {
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);
        BaseDecoration itemDecoration = new BaseDecoration(getContext(), BaseDecoration.VERTICAL);
        itemDecoration.setDrawable(getContext().getDrawable(R.drawable.my_decoration));
        mRecyclerView.addItemDecoration(itemDecoration);

        final EcBottomDelegate ecBottomDelegate = getParentDelegate();
        mRecyclerView.addOnItemTouchListener(IndexItemClickListener.create(ecBottomDelegate));
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initRefreshLayout();
        initRecyclerView();
        initData("getHomepage");
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_index;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            getParentDelegate().getSupportDelegate().start(new SearchDelegate());
        }
    }

    private void initData(String url) {
            RestClient.builder()
                    .url(url)
                    .success(this)
                    .build()
                    .post();
        }

    @Override
    public void onSuccess(String response) {
        mAdapter = MultipleRecyclerAdapter.create(CONVERTER.setJsonData(response));
        mAdapter.setIndexDelegate(this);
        mRecyclerView.setAdapter(mAdapter);
    }
}
